package com.wing.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.Result;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.product.model.response.ProductDetailVO;
import com.wing.enterprise.product.model.response.ProductPackLabelVO;
import com.wing.enterprise.product.model.response.ProductPackVO;
import com.wing.enterprise.product.model.response.ProductUnitVO;
import com.wing.order.dao.InventoryJobDao;
import com.wing.order.dao.InventoryJobProductDao;
import com.wing.order.model.entity.InventoryJob;
import com.wing.order.model.request.InventoryJobForm;
import com.wing.order.model.response.InventoryJobProductVO;
import com.wing.order.model.response.InventoryJobVO;
import com.wing.product.model.entity.Product;
import com.wing.product.service.ProductDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 出/入库作业单记录-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class InventoryJobServiceImpl extends ServiceImpl<InventoryJobDao, InventoryJob> implements InventoryJobService {

    @Autowired
    private InventoryJobProductDao inventoryJobProductDao;
    @Autowired
    private EnterpriseClient productDetailClient;
    @Autowired
    private com.wing.product.dao.ProductDao ProductDao;
    @Autowired
    private com.wing.product.dao.ProductTypeDao productTypeDao;
    @Autowired
    private com.wing.product.dao.ProductUnitDao ProductUnitDao;
    @Autowired
    private ProductDetailService productDetailService;
    @Override
    public int add(InventoryJobForm inventoryJobForm) {

        //校验

        InventoryJob inventoryJob =new InventoryJob();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(InventoryJob.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(inventoryJobForm, inventoryJob, copyOptions);

        return baseMapper.insert(inventoryJob);
    }

    @Override
    public List<InventoryJobVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<InventoryJobVO> pageList4app(Page page, Map<String, Object> params) {
        List<InventoryJobVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public InventoryJobVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<InventoryJobProductVO> getJobProductList(Long jobId) {
        return inventoryJobProductDao.getJobProductList(jobId);
    }

    @Override
    public InventoryJobVO getInventoryJobVOByJobId(Long jobId) {
        return baseMapper.getInventoryJobVOByJobId(jobId);
    }

    @Override
    public List<InventoryJobProductVO> getSignPackSnList(String[] packLabelIdList) {
        List<InventoryJobProductVO> list = new ArrayList<InventoryJobProductVO>();
        for (String item : packLabelIdList) {
            InventoryJobProductVO inventoryJobProductVO = new InventoryJobProductVO();
            //查询包装码
            JsonResult<ProductPackLabelVO> packLabelJson = productDetailClient.getProductPackLabelById(Long.valueOf(item));
            inventoryJobProductVO.setPackLabeId(packLabelJson.getData().getId());
            inventoryJobProductVO.setPackLabelCode(packLabelJson.getData().getCode());//包装编号
            inventoryJobProductVO.setCreateTime(packLabelJson.getData().getCreateTime());//录入时间
            //规格描述
            JsonResult<ProductPackVO> packJson = productDetailClient.getProductPackById(packLabelJson.getData().getPackId());
            inventoryJobProductVO.setPackCode(packJson.getData().getName());
            inventoryJobProductVO.setPackId(packJson.getData().getId());
            //如果入库类型是签收刀具入库 查询cab库
            if (packLabelJson.getData().getInventoryTypeId().name().equals("cutter")) {
                com.wing.product.model.response.ProductDetailVO productDetailVO = productDetailService.getProductDetailBySkuCode(packLabelJson.getData().getSkuCode());
                inventoryJobProductVO.setSkuCode(productDetailVO.getSkuCode());
                inventoryJobProductVO.setSkuSpecCode(productDetailVO.getSkuSpecValueNames());
                //查询商品
                Product product = ProductDao.selectById(productDetailVO.getProductId());
                if(product != null){
                    //单位
                    com.wing.product.model.response.ProductUnitVO productUnitVO = ProductUnitDao.get4appById(product.getUnitId());
                    inventoryJobProductVO.setUnitId(productUnitVO.getId());
                    inventoryJobProductVO.setUnitName(productUnitVO.getName());
                }
            }else{
                JsonResult<ProductDetailVO> jsonResult = productDetailClient.getProductDetailBySkuCode(packLabelJson.getData().getSkuCode());
                if (jsonResult.getCode() == Result.Code.SUCCESS.getCode()) {
                    inventoryJobProductVO.setSkuCode(jsonResult.getData().getSkuCode());//产品编号
                    inventoryJobProductVO.setSkuSpecCode(jsonResult.getData().getSkuSpecValueNames());//规格编码
                }
                JsonResult<ProductUnitVO> unitVo = productDetailClient.getProductUnitById(packLabelJson.getData().getProductUnitId());
                if (unitVo.getCode() == Result.Code.SUCCESS.getCode()) {
                    inventoryJobProductVO.setUnitName(unitVo.getData().getName());//最小单位
                    inventoryJobProductVO.setUnitId(unitVo.getData().getId());
                }
            }
            list.add(inventoryJobProductVO);
        }
        return list;
    }
}
